# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def isPalindrome(self, head: ListNode) -> bool:
        if not head or not head.next:
            return True
        fast=head.next.next
        slow=head
        while fast and fast.next:
            fast=fast.next.next
            slow=slow.next
        if fast:
            fast=slow.next.next
        else:
            fast=slow.next
        slow.next=None
        if head.next:
            tail=head
            head=None
            while tail:
                temp=tail.next
                tail.next=head
                head=tail
                tail=temp
        while head:
            if head.val!=fast.val:
                return False
            head=head.next
            fast=fast.next
        return True